#include "stdafx.h"
#include "YProactorTokenOnConnect.h"
#include "ITcpSession.h"
#include "ITcpSessionCallback.h"
#include <MSWSock.h>
#include <YSimpleSystem/YSimpleSystem.h>

namespace ysimple_network
{
	YProactorTokenOnConnect::YProactorTokenOnConnect(void)
		: m_pCallback(nullptr)
		, m_pSession(nullptr)
	{
	}


	YProactorTokenOnConnect::~YProactorTokenOnConnect(void)
	{
	}

	bool YProactorTokenOnConnect::onCompleted( bool completionStatus, LONG_PTR completionKey, DWORD dwBytesTransfered )
	{		
		setsockopt(m_pSession->getSocket(), SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);	
		
		m_pCallback->onConnected(*m_pSession, completionStatus);
		m_pSession->release();
		return true;
	}
	
	void YProactorTokenOnConnect::beginInit( IConnectCallback& callback, ITCPSession& session )
	{
		resetOverlapped();

		m_pCallback = &callback;
		m_pSession = &session;
		m_pSession->addRef();
	}

	void YProactorTokenOnConnect::endInit( bool success )
	{
		if (success == false)
		{
			m_pSession->release();
		}
	}

}


